<?php
/**
 * Created by Dumitru Russu
 * User: developer
 * Date: 8/30/12
 * Time: 10:48 AM
 * To change this template use File | Settings | File Templates.
 */
class Page_Be_Products_CoverTypes extends Page
{
    /**
     * @var Db_Products_CoverTypes
     */
    protected $tableObject;

    function __construct() {
        $this->idTableObject = get('id', 'int');
        $this->actionsTableObject = array(
            'addEditAction',
            'saveAction'
        );
        $this->classTableObject = 'Db_Products_CoverTypes';
        parent::__construct();
    }

    public function defaultAction() {
		if (perm('PRODUCTS_COVER_TYPES_EDIT')) {
        $this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit');
		}
        $allCoverTypes = Db_Products_CoverTypes::getAllCoverTypes();

        self::$TPL->assign('allCoverTypes', $allCoverTypes);
        self::$TPL->setBodyTemplate('list_cover_types');
        return array(
            'type' => 'display'
        );
    }

    public function addEditAction() {
		perm('PRODUCTS_COVER_TYPES_EDIT', self::$PAGE['p_url']);
        if(!empty($this->tableObject->objectArray->id)) {
            $this->addBreadcrumb(t(T_FORM_BUTTON, 'edit') . ' &quot;' . t( T_COVER_TYPES_TITLE ,$this->tableObject->objectArray->ct_name) . '&quot;', '?action=addEdit&id=' . $this->idTableObject, true);
            self::$TPL->assign('coverTypes', $this->tableObject->objectArray);
        } else {
            $this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit', true);
        }
        self::$TPL->setBodyTemplate('add_edit_cover_type');
        return array(
            'type' => 'display'
        );
    }

    public function saveAction() {
		perm('PRODUCTS_COVER_TYPES_EDIT', self::$PAGE['p_url']);
        $coverTypeName = strtolower(post('ct_name', 'string'));
        $this->message = t(T_FORM_MSG, 'success');
        if(!empty($coverTypeName)){
            $this->tableObject->ct_name = $coverTypeName;
            $this->tableObject->ct_hard_cover = post('ct_hard_cover', 'int');
            if(!$this->tableObject->save()) {

                $this->error = true;
                $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
            }
        } else {
            $this->error = true;
            $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
        }
        return array(
            'type' => 'redirect',
            'query' => false
        );
    }

    public function deleteAction() {
		perm('PRODUCTS_COVER_TYPES_EDIT', self::$PAGE['p_url']);
        $this->message = t(T_FORM_MSG, 'success');
            if(!Db_Products_CoverTypes::abstractDelete($this->idTableObject)) {
                $this->error = true;
                $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
            }
        return array(
            'type' => 'redirect',
            'query' => false
        );
    }
}
